package com.elvin.zoe.template;

import static com.elvin.zoe.template.HtmlTemplateResources.footGroupTemplate;
import static com.elvin.zoe.template.HtmlTemplateResources.head;
import static com.elvin.zoe.template.HtmlTemplateResources.headGroupTemplate;
import static com.elvin.zoe.template.HtmlTemplateResources.resourceLinks;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.elvin.zoe.pojo.Conversation;
import com.elvin.zoe.pojo.Message;
import com.elvin.zoe.pojo.SmsMessage;

public class SingleHtmlTemplate extends HtmlTemplate {

    public SingleHtmlTemplate(Conversation conversation) {
        super(conversation);
    }

    public String generateHtml() throws ParseException{


        StringBuffer stringBuffer = new StringBuffer(resourceLinks);
        stringBuffer.append(head);
        stringBuffer.append("<body>");
        stringBuffer.append(headGroupTemplate);

        stringBuffer.append(generateBody());

        stringBuffer.append("</body>");
        return stringBuffer.toString();
    }

    protected String generateBody() throws ParseException{

        List<Message> messages = conversation.getMessages();

        Collections.sort(messages, new Comparator<Message>(){
            @Override
            public int compare(Message m1, Message m2) {
                if(m1 == null || m2 == null)
                    return 0;
                if(m1.getDateTime() == null || m2.getDateTime() == null)
                    return 0;
                return m1.getDateTime().compareTo(m2.getDateTime());
            }
        });

        int smsMessageCount = 0;
        int callMessageCount = 0;
        Map<Date, List<Message>> mapMessageDate2Message = new TreeMap<Date, List<Message>>();
        for(Message message : messages){

            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(message.getDateTime());
            calendar.add(Calendar.HOUR, -6);

            Date day = dateFormatDateOnly.parse(dateFormatDateOnly.format(calendar.getTime()));
            List<Message> daylyMessages = mapMessageDate2Message.get(day);
            if(daylyMessages == null){
                daylyMessages = new ArrayList<Message>();
                mapMessageDate2Message.put(day, daylyMessages);
            }
            daylyMessages.add(message);

            if(message instanceof SmsMessage)
                smsMessageCount ++;
            else
                callMessageCount ++;

//            if (smsMessageCount >= 20)
//                break;
        }

        System.out.println("mapMessageDate2Message Days - " + mapMessageDate2Message.size());

        StringBuffer stringBuffer = new StringBuffer();
        for(Map.Entry<Date,List<Message>> entry : mapMessageDate2Message.entrySet()){
            stringBuffer.append(generateDaylyGroupHtml(entry.getKey(), entry.getValue()));
        }

        stringBuffer.append(footGroupTemplate);
        return stringBuffer.toString();
    }
}
